/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.oop.example.work.esport;

import com.oop.example.xml.Block;
import com.oop.example.xml.Entry;
import com.oop.example.xml.Entry.Content;
import com.oop.example.xml.Entry.Heading;
import com.oop.example.xml.Homonym;
import com.oop.example.xml.Near;
import com.oop.example.xml.ObjectFactory;
import com.oop.example.xml.PhraseWithAttribs;
import com.oop.example.xml.Reference;
import com.oop.example.xml.Sense;
import com.oop.example.xml.SubSense;
import com.oop.example.xml.Translation;
import com.oop.example.xml.Variant;
import javax.xml.bind.JAXBElement;

/**
 *
 * @author -
 */
public class AlternativeEntryCreator {

    private ObjectFactory factory = null;

    public AlternativeEntryCreator(ObjectFactory factory) {
        this.factory = factory;
    }

    /**
     * Добавляет статью из альтернативного заголовка
     *
     * @param header заголовок статьи
     * @param refToHeader заголовок статьи на которую следует сослаться
     * @param refNum нумератор омонима, на который следует сослаться
     */
    public Entry createAlterantiveEntry(String header, String refToHeader, String refNum) {
        getNear();//создает пустую структуру статьи
        Reference.External external = factory.createReferenceExternal();
        Reference ref = factory.createReference();
        ref.setExternal(external);
        ref.setDisplayName(refToHeader);
        ref.setReferencedLexeme(refToHeader);
        //ref.setReferenceTail(refNum);
        //ref.setViewReferenceTail(refNum);
        JAXBElement<Reference> jbeRef = factory.createNearReference(ref);
        PhraseWithAttribs see = factory.createPhraseWithAttribs();
        see.getPhraseChildren().add(jbeRef);
        JAXBElement<PhraseWithAttribs> jbeSee = factory.createSubSenseSee(see);
        getSubSense().getSubSenseChildren().add(jbeSee);
        getHeadingSee().setValue(header.trim());
        return entry;
    }

    public Entry getEntry() {
        if (entry == null) {
            entry = factory.createEntry();
            entry.setEntryAttributes(factory.createEntryEntryAttributes());
            entry.getEntryAttributes().setIndexInclude(true);
        }
        return entry;
    }

    public Block getHeadingSee() {
        if (entryHeading == null) {
            entryHeading = factory.createEntryHeading();
            Heading.Vocable entryHeadingVocable = factory.createEntryHeadingVocable();
            //entryHeadingVocable.setIsPrimary(true);
            Entry.Heading.Vocable.Part entryHeadingVocablePart = factory.createEntryHeadingVocablePart();
            headingSee = factory.createBlock();
            JAXBElement<Block> simple = factory.createEntryHeadingVocablePartSimple(headingSee);
            entryHeadingVocablePart.getPartChildren().add(simple);
            entryHeadingVocable.getPart().add(entryHeadingVocablePart);
            entryHeading.getVocable().add(entryHeadingVocable);
            getEntry().setHeading(entryHeading);
        }
        return headingSee;
    }

    public Entry.Content getContent() {
        if (content == null) {
            content = factory.createEntryContent();
            getEntry().setContent(content);
        }
        return content;
    }

    private Homonym getHomonym() {
        if (homonym == null) {
            homonym = factory.createHomonym();
            homonym.setNumerator("I");
            getContent().getClassifierOrPronunciationOrHomonym().add(factory.createEntryContentHomonym(homonym));
        }
        return homonym;
    }

    private Variant getVariant() {
        if (variant == null) {
            variant = factory.createVariant();
            variant.setNumerator("1.");
            getHomonym().getPronunciationOrClassifierOrTranslation().add(factory.createHomonymVariant(variant));
        }
        return variant;
    }

    private Sense getSense() {
        if (sense == null) {
            sense = factory.createSense();
            sense.setNumerator("1)");
            getVariant().getPronunciationOrClassifierOrTranslation().add(factory.createVariantSense(sense));
        }
        return sense;
    }

    private SubSense getSubSense() {
        String numerator = "a)";
        if (subSense == null) {
            subSense = factory.createSubSense();
            subSense.setNumerator(numerator);
            getSense().getPronunciationOrClassifierOrTranslation().add(factory.createSenseSubSense(subSense));
        }
        return subSense;
    }

    private Translation getTranslation() {
        if (translation == null) {
            translation = factory.createTranslation();
            getSubSense().getSubSenseChildren().add(factory.createSubSenseTranslation(translation));
        }
        return translation;
    }

    private Translation.Far getFar() {
        if (far == null) {
            far = factory.createTranslationFar();
            getTranslation().getFar().add(far);
        }
        return far;
    }

    private Near getNear() {
        if (near == null) {
            near = factory.createNear();
            getFar().getNear().add(near);
        }
        return near;
    }
    private Heading entryHeading = null;
    private Content content = null;
    private Homonym homonym = null;
    private Variant variant = null;
    private Sense sense = null;
    private SubSense subSense = null;
    private Translation translation = null;
    private Translation.Far far = null;
    private Near near = null;
    private Entry entry = null;
    private Block headingSee = null;
}
